package com.example.dessert.controller.fore;

import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.Address;
import com.example.dessert.entity.Region;
import com.example.dessert.service.AddressService;
import com.example.dessert.service.RegionService;
import com.example.dessert.util.ResponsePojo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: IndexController
 * @Description:
 * @Author li
 * @Date 2022/1/4
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/address")
public class AddressController extends BaseController {

    @Resource
    private AddressService addressService;
    @Resource
    private RegionService regionService;

    /**
     * 用户地址列表
     */
    @GetMapping("/list")
    @LoginCheck
    public String info(HttpSession session, Model model) {

        Object userId = session.getAttribute("userId");
        logger.info("Query user address userId:{}", userId);

        List<Address> address = addressService.queryAddressList((Long) userId);
        logger.info("Query user address results :{}", address);

        model.addAttribute("address", address);

        return "fore/address/list";

    }


    /**
     * 删除地址信息
     */
    @PostMapping("/delete")
    @LoginCheck
    @ResponseBody
    public ResponsePojo delete(HttpSession session,@RequestParam Map<String, Object> map) {

        Long id = Long.valueOf(map.get("id").toString());
        int result =  addressService.deleteAddressById(id);
        logger.info("Delete user address information result :{}", result);

        return ResponsePojo.ok();

    }


    /**
     * 添加用户地址
     */
    @GetMapping("/create")
    @LoginCheck
    public String createAddress(Model model) {

        List<Region> provinces = regionService.queryChildrenById(0L);
        model.addAttribute("provinces", provinces);

        return "fore/address/create";

    }

    /**
     * 添加地址信息
     */
    @PostMapping("/create")
    @LoginCheck
    @ResponseBody
    public ResponsePojo storeAddress(HttpSession session,@RequestParam Map<String, Object> map) {

        Object userId = session.getAttribute("userId");

        String contact = (String)map.get("contact");
        String mobile = (String)map.get("mobile");
        String province = (String)map.get("province");
        String city = (String)map.get("city");
        String county = (String)map.get("county");
        String address = (String)map.get("address");

        Address cAddress = new Address();
        cAddress.setUserId((Long) userId)
                .setContact(contact)
                .setMobile(mobile)
                .setProvince(province)
                .setCity(city)
                .setCounty(county)
                .setAddress(address);

        Long result =  addressService.storeAddress(cAddress);
        logger.info("Create User Address Information Results:{}", result);

        return ResponsePojo.ok();

    }


    /**
     * 查询下属地区
     */
    @PostMapping("/childrenArea")
    @ResponseBody
    public ResponsePojo childrenArea(@RequestParam Map<String, Object> map,Model model) {

        Long id = Long.parseLong(map.get("id").toString());
        logger.info("query area parent id:{}", id);

        List<Region> provinces = regionService.queryChildrenById(id);
        logger.info("Query the results of subordinate regions by id:{}", provinces);

        return ResponsePojo.ok(provinces);

    }






}
